//
//  PictureViewController.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "PictureViewController.h"
#import "PictureTableViewCell.h"
#import "PictureHeaderView.h"
#import "PushTieziViewController.h"
#import "PictureModel.h"

@interface PictureViewController ()<UITableViewDelegate,UITableViewDataSource,PictureCellDalegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<PictureModel *>* pictureListArray;

@end

@implementation PictureViewController

- (void)setPictureListArray:(NSArray<PictureModel *> *)pictureListArray{
    
    _pictureListArray = pictureListArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [PictureModel pictureWithSucess:^(NSArray<PictureModel *> *array) {
        
        self.pictureListArray = array;
    } error:^{
        
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GetPictureData) name:@"GetPictureData" object:nil];
    
    [self setupNav];
    [self setupUI];
}



- (void)setupNav{
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
    //    [backButton1 setTitle:@"登录" forState:UIControlStateNormal];
    [backButton1 setImage:ImageNamed(@"xiangxiang") forState:UIControlStateNormal];
    [backButton1 addTarget:self action:@selector(pushToShareMessage) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}



- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 80) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 230 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[PictureTableViewCell class] forCellReuseIdentifier:@"PictureListCell"];
    [self.tableView registerClass:[PictureHeaderView class] forHeaderFooterViewReuseIdentifier:@"PictureHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.pictureListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureListCell" forIndexPath:indexPath];
    
        cell.model = self.pictureListArray[indexPath.row];
    cell.delegate = self;
    [cell.hiddenButton addTarget:self action:@selector(clickHiddenButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.hiddenButton.tag = indexPath.row;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 145 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    PictureHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PictureHeaderView"];
    
    return header;
}

- (void)didCellLikeButton:(NSString *)string{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:string
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              //                                                                          NSLog(@"action = %@", action);
                                                          }];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}


- (void)pushToShareMessage{
    
    HIDDEN_TAB;
    
    PushTieziViewController * pushVC= [PushTieziViewController new];
    
    [self.navigationController pushViewController:pushVC animated:YES];
    
    SHOW_TAB;
}

- (void)GetPictureData{
    
    [PictureModel pictureWithSucess:^(NSArray<PictureModel *> *array) {
        
        self.pictureListArray = array;
    } error:^{
        
    }];
}


- (void)clickHiddenButton:(UIButton *)button{
    
    NSInteger count = button.tag;
    
    PictureModel * model = self.pictureListArray[count];
    NSString * ID = model.ID;
    NSString * token = DEF_GET_OBJECT(@"token");
    
    NSDictionary * dict = @{@"token":token,@"note_id":ID,@"shield":@"1"};
    
    [[NetworkTool sharedTools] POST:@"/index/Note/shield" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"===%@",responseObject);
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            [PictureModel pictureWithSucess:^(NSArray<PictureModel *> *array) {
                
                self.pictureListArray = array;
            } error:^{
                
            }];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
